package basic.com.liz.controller.business;

import java.util.Date;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import basic.com.liz.bean.entity.business.Sms;
import basic.com.liz.common.utils.HttpResult;
import basic.com.liz.common.utils.Servlets;
import basic.com.liz.controller.system.SystemBasicController;
import basic.com.liz.service.business.SmsService;

/**
 * <短信处理>
 * 
 * @author 陆小凤
 * @version [1.0, 2015年9月11日]
 */
@Slf4j
@Controller
@RequestMapping(value = "/sms")
public class SMSController extends BusinessBasicController
{
    
    @Autowired
    private SmsService smsService;
    
    @Autowired
    private SmsSendApiWorker worker;
    
    /**
     * <查询短信信息>
     * 
     * @param pageNumber
     * @param pageSize
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "list")
    public String smsList(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
        @RequestParam(value = "numPerPage", defaultValue = "20") int numPerPage, Model model, ServletRequest request)
    {
        // 获取查询条件
        Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
        try
        {
            // 根据条件查询角色信息
            Page<Sms> smsList = smsService.getSmsList(searchParams, pageNum, numPerPage);
            model.addAttribute("smsList", smsList.getContent());
            model.addAttribute("totalCount", smsList.getTotalElements());
            model.addAttribute("pageNum", pageNum);
            model.addAttribute("numPerPage", numPerPage);
            
        }
        catch (Exception e)
        {
            log.error("Enter func[smsList] catch exception while get systemConfigList ", e);
        }
        return "business/sms/smsList";
    }
    
    /**
     * <进入短信添加页面>
     * 
     * @return
     */
    @RequestMapping(value = "add")
    public String addConfig(ModelMap model)
    {
        return "business/sms/smsInfo";
    }
    
    /**
     * <保存>
     * 
     * @param menu
     * @param model
     * @param response
     * @return
     */
    @RequestMapping(value = "saveSms")
    @ResponseBody
    public HttpResult saveSms(@Valid @ModelAttribute("sms") Sms sms, ModelMap model, HttpServletRequest request,
        HttpServletResponse response)
    {
        HttpResult result = new HttpResult();
        try
        {
            sms.setExtend("123456");
            sms.setSmsType("normal");
            sms.setCreateTime(new Date());
            smsService.save(sms);
            // 添加到执行发送短信的队列
            worker.putToQueue(sms);
            // 返回正确的的结果
            result = getCorrectResult();
        }
        catch (Exception e)
        {
            // 返回错误的结果
            result = getErrorResult();
            log.error("Enter func[saveSms] catch exception", e);
        }
        return result;
        
    }
    
    /**
     * <删除系统参数配置>
     * 
     * @param response
     * @param request
     */
    @RequestMapping(value = "delete")
    @ResponseBody
    public HttpResult deleteById(@RequestParam(value = "id") Long id)
    {
        HttpResult result = new HttpResult();
        
        try
        {
            smsService.deleteById(id);
            // 返回正确的的结果
            result = getCorrectResult();
        }
        catch (Exception e)
        {
            // 返回错误的结果
            result = getErrorResult();
            log.info("Enter func[deleteById] catch exception", e);
        }
        return result;
    }
    
}
